home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / fill.el.z / fill.el
Encoding:
Text File  |  1998-05-21  |  39.4 KB  |  1,059 lines

  1. ;;; fill.el --- fill commands for XEmacs.
  2.  
  3. ;; Copyright (C) 1985, 86, 92, 94, 95, 1997 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: wp
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  22. ;; 02111-1307, USA.
  23.  
  24. ;;; Synched up with: FSF 19.34.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; All the commands for filling text.  These are documented in the XEmacs
  29. ;; Reference Manual.
  30.  
  31. ;; 97/3/14 Jareth Hein (jhod@po.iijnet.or.jp) added functions for kinsoku (asian text
  32. ;; line break processing)
  33. ;; 97/06/11 Steve Baur (steve@altair.xemacs.org) converted broken
  34. ;;  following-char/preceding-char calls to char-after/char-before.
  35.  
  36. ;;; Code:
  37.  
  38. (defconst fill-individual-varying-indent nil
  39.   "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
  40. Non-nil means changing indent doesn't end a paragraph.
  41. That mode can handle paragraphs with extra indentation on the first line,
  42. but it requires separator lines between paragraphs.
  43. A value of nil means that any change in indentation starts a new paragraph.")
  44.  
  45. (defconst sentence-end-double-space t
  46.   "*Non-nil means a single space does not end a sentence.
  47. This variable applies only to filling, not motion commands.  To
  48. change the behavior of motion commands, see `sentence-end'.")
  49.  
  50. (defconst colon-double-space nil
  51.   "*Non-nil means put two spaces after a colon when filling.")
  52.  
  53. (defvar fill-paragraph-function nil
  54.   "Mode-specific function to fill a paragraph, or nil if there is none.
  55. If the function returns nil, then `fill-paragraph' does its normal work.")
  56.  
  57. (defun set-fill-prefix ()
  58.   "Set the fill prefix to the current line up to point.
  59. Filling expects lines to start with the fill prefix and
  60. reinserts the fill prefix in each resulting line."
  61.   (interactive)
  62.   (setq fill-prefix (buffer-substring
  63.              (save-excursion (move-to-left-margin) (point))
  64.              (point)))
  65.   (if (equal fill-prefix "")
  66.       (setq fill-prefix nil))
  67.   (if fill-prefix
  68.       (message "fill-prefix: \"%s\"" fill-prefix)
  69.     (message "fill-prefix cancelled")))
  70.  
  71. (defconst adaptive-fill-mode t
  72.   "*Non-nil means determine a paragraph's fill prefix from its text.")
  73.  
  74. ;; #### - this is still weak.  Yeah, there's filladapt, but this should
  75. ;; still be better...  --Stig
  76. (defconst adaptive-fill-regexp (purecopy "[ \t]*\\([#;>*]+ +\\)?")
  77.   "*Regexp to match text at start of line that constitutes indentation.
  78. If Adaptive Fill mode is enabled, whatever text matches this pattern
  79. on the second line of a paragraph is used as the standard indentation
  80. for the paragraph.  If the paragraph has just one line, the indentation
  81. is taken from that line.")
  82.  
  83. (defvar adaptive-fill-function nil
  84.   "*Function to call to choose a fill prefix for a paragraph.
  85. This function is used when `adaptive-fill-regexp' does not match.")
  86.  
  87. ;; Added for kinsoku processing. Use this instead of 
  88. ;; (skip-chars-backward "^ \t\n")
  89. ;; (skip-chars-backward "^ \n" linebeg)
  90. (defun fill-move-backward-to-break-point (regexp &optional lim)
  91.   (let ((opoint (point)))
  92.     ;; 93.8.23 by kawamoto@ics.es.osaka-u.ac.jp
  93.     ;;  case of first 'word' being longer than fill-column
  94.     (if (not (re-search-backward regexp lim 'move))
  95.     nil
  96.       ;; we have skipped backward SPC or WAN (word-across-newline).  So move point forward again.
  97.       (forward-char)
  98.       (if (< opoint (point))
  99.       (forward-char -1)))))
  100.  
  101. ;; Added for kinsoku processing. Use instead of
  102. ;; (re-search-forward "[ \t]" opoint t)
  103. ;; (skip-chars-forward "^ \n")
  104. ;; (skip-chars-forward "^ \n")
  105. (defun fill-move-forward-to-break-point (regexp &optional lim)
  106.   (let ((opoint (point)))
  107.     (if (not (re-search-forward regexp lim 'move))
  108.     nil
  109.       (forward-char -1)
  110.       (if (< (point) opoint)
  111.       (forward-char))))
  112.   (if (featurep 'mule) (kinsoku-process-extend)))
  113.  
  114. (defun fill-end-of-sentence-p ()
  115.   (save-excursion
  116.     (skip-chars-backward " ]})\"'")
  117.     (memq (char-before (point)) '(?. ?? ?!))))
  118.  
  119. (defun current-fill-column ()
  120.   "Return the fill-column to use for this line.
  121. The fill-column to use for a buffer is stored in the variable `fill-column',
  122. but can be locally modified by the `right-margin' text property, which is
  123. subtracted from `fill-column'.
  124.  
  125. The fill column to use for a line is the first column at which the column
  126. number equals or exceeds the local fill-column - right-margin difference."
  127.   (save-excursion
  128.     (if fill-column
  129.     (let* ((here (progn (beginning-of-line) (point)))
  130.            (here-col 0)
  131.            (eol (progn (end-of-line) (point)))
  132.            margin fill-col change col)
  133.       ;; Look separately at each region of line with a different right-margin.
  134.       (while (and (setq margin (get-text-property here 'right-margin)
  135.                 fill-col (- fill-column (or margin 0))
  136.                 change (text-property-not-all
  137.                     here eol 'right-margin margin))
  138.               (progn (goto-char (1- change))
  139.                  (setq col (current-column))
  140.                  (< col fill-col)))
  141.         (setq here change
  142.           here-col col))
  143.       (max here-col fill-col)))))
  144.  
  145. (defun canonically-space-region (beg end)
  146.   "Remove extra spaces between words in region.
  147. Leave one space between words, two at end of sentences or after colons
  148. \(depending on values of `sentence-end-double-space' and `colon-double-space').
  149. Remove indentation from each line."
  150.   (interactive "r")
  151.   ;;;### 97/3/14 jhod: Do I have to add anything here for kinsoku?
  152.   (save-excursion
  153.     (goto-char beg)
  154.     ;; XEmacs - (ENE/stig from fa-extras.el): Skip the start of a comment.
  155.     (and comment-start-skip
  156.      (looking-at comment-start-skip)
  157.      (goto-char (match-end 0)))
  158.     ;; Nuke tabs; they get screwed up in a fill.
  159.     ;; This is quick, but loses when a tab follows the end of a sentence.
  160.     ;; Actually, it is difficult to tell that from "Mr.\tSmith".
  161.     ;; Blame the typist.
  162.     (subst-char-in-region beg end ?\t ?\ )
  163.     (while (and (< (point) end)
  164.         (re-search-forward "   *" end t))
  165.       (delete-region
  166.        (+ (match-beginning 0)
  167.       ;; Determine number of spaces to leave:
  168.       (save-excursion
  169.         (skip-chars-backward " ]})\"'")
  170.         (cond ((and sentence-end-double-space
  171.             (memq (char-before (point)) '(?. ?? ?!)))  2)
  172.           ((and colon-double-space
  173.             (eq (char-before (point)) ?:))  2)
  174.           ((char-equal (char-before (point)) ?\n)  0)
  175.           (t 1))))
  176.        (match-end 0)))
  177.     ;; Make sure sentences ending at end of line get an extra space.
  178.     ;; loses on split abbrevs ("Mr.\nSmith")
  179.     (goto-char beg)
  180.     (while (and (< (point) end)
  181.         (re-search-forward "[.?!][])}\"']*$" end t))
  182.       ;; We insert before markers in case a caller such as
  183.       ;; do-auto-fill has done a save-excursion with point at the end
  184.       ;; of the line and wants it to stay at the end of the line.
  185.       (insert ? ))))
  186. ;; XEmacs: we don't have this function.
  187. ;; (insert-before-markers-and-inherit ? ))))
  188.  
  189. ;; XEmacs -- added DONT-SKIP-FIRST.  Port of older code changes by Stig.
  190. ;; #### probably this junk is broken -- do-auto-fill doesn't actually use
  191. ;; it.  If so, it should be removed.
  192.  
  193. (defun fill-context-prefix (from to &optional first-line-regexp
  194.                  dont-skip-first)
  195.   "Compute a fill prefix from the text between FROM and TO.
  196. This uses the variables `adaptive-fill-prefix' and `adaptive-fill-function'.
  197. If FIRST-LINE-REGEXP is non-nil, then when taking a prefix from the
  198. first line, insist it must match FIRST-LINE-REGEXP."
  199.   (save-excursion
  200.     (goto-char from)
  201.     (if (eolp) (forward-line 1))
  202.     ;; Move to the second line unless there is just one.
  203.     (let ((firstline (point))
  204.       ;; Non-nil if we are on the second line.
  205.       at-second
  206.       result)
  207.       ;; XEmacs change
  208.       (if (not dont-skip-first)
  209.       (forward-line 1))
  210.       (if (>= (point) to)
  211.       (goto-char firstline)
  212.     (setq at-second t))
  213.       (move-to-left-margin)
  214.       ;; XEmacs change
  215.       (let ((start (point))
  216.         ; jhod: no longer used?
  217.         ;(eol (save-excursion (end-of-line) (point)))
  218.         )
  219.     (setq result
  220.           (if (not (looking-at paragraph-start))
  221.           (cond ((and adaptive-fill-regexp (looking-at adaptive-fill-regexp))
  222.              (buffer-substring-no-properties start (match-end 0)))
  223.             (adaptive-fill-function (funcall adaptive-fill-function)))))
  224.     (and result
  225.          (or at-second
  226.          (null first-line-regexp)
  227.          (string-match first-line-regexp result))
  228.          result)))))
  229.  
  230. ;; XEmacs (stig) - this is pulled out of fill-region-as-paragraph so that it
  231. ;; can also be called from do-auto-fill
  232. ;; #### But it's not used there.  Chuck pulled it out because it broke things.
  233. (defun maybe-adapt-fill-prefix (&optional from to dont-skip-first)
  234.   (if (and adaptive-fill-mode
  235.        (or (null fill-prefix) (string= fill-prefix "")))
  236.       (setq fill-prefix (fill-context-prefix from to nil dont-skip-first))))
  237.  
  238. (defun fill-region-as-paragraph (from to &optional justify
  239.                       nosqueeze squeeze-after)
  240.   "Fill the region as one paragraph.
  241. It removes any paragraph breaks in the region and extra newlines at the end,
  242. indents and fills lines between the margins given by the
  243. `current-left-margin' and `current-fill-column' functions.
  244. It leaves point at the beginning of the line following the paragraph.
  245.  
  246. Normally performs justification according to the `current-justification'
  247. function, but with a prefix arg, does full justification instead.
  248.  
  249. From a program, optional third arg JUSTIFY can specify any type of
  250. justification.  Fourth arg NOSQUEEZE non-nil means not to make spaces
  251. between words canonical before filling.  Fifth arg SQUEEZE-AFTER, if non-nil,
  252. means don't canonicalize spaces before that position.
  253.  
  254. If `sentence-end-double-space' is non-nil, then period followed by one
  255. space does not end a sentence, so don't break a line there."
  256.   (interactive
  257.    (progn
  258.      ;; XEmacs addition:
  259.      (barf-if-buffer-read-only nil (region-beginning) (region-end))
  260.      (list (region-beginning) (region-end)
  261.        (if current-prefix-arg 'full))))
  262.   ;; Arrange for undoing the fill to restore point.
  263.   (if (and buffer-undo-list (not (eq buffer-undo-list t)))
  264.       (setq buffer-undo-list (cons (point) buffer-undo-list)))
  265.  
  266.   ;; Make sure "to" is the endpoint.
  267.   (goto-char (min from to))
  268.   (setq to   (max from to))
  269.   ;; Ignore blank lines at beginning of region.
  270.   (skip-chars-forward " \t\n")
  271.  
  272.   (let ((from-plus-indent (point))
  273.     (oneleft nil))
  274.  
  275.     (beginning-of-line)
  276.     (setq from (point))
  277.   
  278.     ;; Delete all but one soft newline at end of region.
  279.     ;; And leave TO before that one.
  280.     (goto-char to)
  281.     (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
  282.       (if (and oneleft
  283.            (not (and use-hard-newlines
  284.              (get-text-property (1- (point)) 'hard))))
  285.       (delete-backward-char 1)
  286.     (backward-char 1)
  287.     (setq oneleft t)))
  288.     (setq to (point))
  289.  
  290.     ;; If there was no newline, and there is text in the paragraph, then
  291.     ;; create a newline.
  292.     (if (and (not oneleft) (> to from-plus-indent))
  293.     (newline))
  294.     (goto-char from-plus-indent))
  295.  
  296.   (if (not (> to (point)))
  297.       nil ; There is no paragraph, only whitespace: exit now.
  298.  
  299.     (or justify (setq justify (current-justification)))
  300.  
  301.     ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
  302.     (let ((fill-prefix fill-prefix))
  303.       ;; Figure out how this paragraph is indented, if desired.
  304.       ;; XEmacs: move some code here to a separate function.
  305.       (maybe-adapt-fill-prefix from to t)
  306.  
  307.       (save-restriction
  308.     (goto-char from)
  309.     (beginning-of-line)
  310.     (narrow-to-region (point) to)
  311.  
  312.     (if (not justify)        ; filling disabled: just check indentation
  313.         (progn
  314.           (goto-char from)
  315.           (while (not (eobp))
  316.         (if (and (not (eolp))
  317.              (< (current-indentation) (current-left-margin)))
  318.             (indent-to-left-margin))
  319.         (forward-line 1)))
  320.  
  321.       (if use-hard-newlines
  322.           (remove-text-properties from (point-max) '(hard nil)))
  323.       ;; Make sure first line is indented (at least) to left margin...
  324.       (if (or (memq justify '(right center))
  325.           (< (current-indentation) (current-left-margin)))
  326.           (indent-to-left-margin))
  327.       ;; Delete the fill prefix from every line except the first.
  328.       ;; The first line may not even have a fill prefix.
  329.       (goto-char from)
  330.       (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
  331.                (concat "[ \t]*"
  332.                    (regexp-quote fill-prefix)
  333.                    "[ \t]*"))))
  334.         (and fpre
  335.          (progn
  336.            (if (>= (+ (current-left-margin) (length fill-prefix))
  337.                (current-fill-column))
  338.                (error "fill-prefix too long for specified width"))
  339.            (goto-char from)
  340.            (forward-line 1)
  341.            (while (not (eobp))
  342.              (if (looking-at fpre)
  343.              (delete-region (point) (match-end 0)))
  344.              (forward-line 1))
  345.            (goto-char from)
  346.            (if (looking-at fpre)
  347.                (goto-char (match-end 0)))
  348.            (setq from (point)))))
  349.       ;; Remove indentation from lines other than the first.
  350.       (beginning-of-line 2)
  351.       (indent-region (point) (point-max) 0)
  352.       (goto-char from)
  353.  
  354.       ;; FROM, and point, are now before the text to fill,
  355.       ;; but after any fill prefix on the first line.
  356.  
  357.       ;; Make sure sentences ending at end of line get an extra space.
  358.       ;; loses on split abbrevs ("Mr.\nSmith")
  359.       (while (re-search-forward "[.?!][])}\"']*$" nil t)
  360.         ;; XEmacs change (no insert-and-inherit)
  361.         (or (eobp) (insert ?\  ?\ )))
  362.       (goto-char from)
  363.       (skip-chars-forward " \t")
  364.       ;; Then change all newlines to spaces.
  365.       ;;; 97/3/14 jhod: Kinsoku change
  366.       ;; Spacing is not necessary for charcters of no word-separater.
  367.       ;; The regexp word-across-newline is used for this check.
  368.       (if (not (and (featurep 'mule)
  369.             (stringp word-across-newline)))
  370.           (subst-char-in-region from (point-max) ?\n ?\ )
  371.         ;;
  372.         ;; WAN     +NL+WAN       --> WAN            + WAN
  373.         ;; not(WAN)+NL+WAN       --> not(WAN)       + WAN
  374.         ;; WAN     +NL+not(WAN)  --> WAN            + not(WAN)
  375.         ;; SPC     +NL+not(WAN)  --> SPC            + not(WAN)
  376.         ;; not(WAN)+NL+not(WAN)  --> not(WAN) + SPC + not(WAN)
  377.         ;;
  378.         (goto-char from)
  379.         (end-of-line)
  380.         (while (not (eobp))
  381.           ;; Insert SPC only when point is between nonWAN.  Insert
  382.           ;; before deleting to preserve marker if possible.
  383.           (if (or (prog2        ; check following char.
  384.               (forward-char)    ; skip newline
  385.               (or (eobp)
  386.                   (looking-at word-across-newline))
  387.             (forward-char -1))
  388.               (prog2        ; check previous char.
  389.               (forward-char -1)
  390.               (or (eq (char-after (point)) ?\ )
  391.                   (looking-at word-across-newline))
  392.             (forward-char)))
  393.           nil
  394.         (insert ?\ ))
  395.           (delete-char 1)        ; delete newline
  396.           (end-of-line)))
  397.       ;; end patch
  398.       (goto-char from)
  399.       (skip-chars-forward " \t")
  400.        (if (and nosqueeze (not (eq justify 'full)))
  401.           nil
  402.         (canonically-space-region (or squeeze-after (point)) (point-max))
  403.         (goto-char (point-max))
  404.         (delete-horizontal-space)
  405.         ;; XEmacs change (no insert-and-inherit)
  406.         (insert " "))
  407.       (goto-char (point-min))
  408.  
  409.       ;; This is the actual filling loop.
  410.       (let ((prefixcol 0) linebeg
  411.         (re-break-point (if (featurep 'mule)
  412.                     (concat "[ \n\t]\\|" word-across-newline)
  413.                   "[ \n\t]")))
  414.         (while (not (eobp))
  415.           (setq linebeg (point))
  416.           (move-to-column (1+ (current-fill-column)))
  417.           (if (eobp)
  418.           (or nosqueeze (delete-horizontal-space))
  419.         ;; Move back to start of word.
  420.         ;; 97/3/14 jhod: Kinsoku
  421.         ;(skip-chars-backward "^ \n" linebeg)
  422.         (fill-move-backward-to-break-point re-break-point linebeg)
  423.         ;; end patch
  424.         ;; Don't break after a period followed by just one space.
  425.         ;; Move back to the previous place to break.
  426.         ;; The reason is that if a period ends up at the end of a line,
  427.         ;; further fills will assume it ends a sentence.
  428.         ;; If we now know it does not end a sentence,
  429.         ;; avoid putting it at the end of the line.
  430.         (if sentence-end-double-space
  431.             (while (and (> (point) (+ linebeg 2))
  432.                 (eq (char-before (point)) ?\ )
  433.                 (not (eq (char-after (point)) ?\ ))
  434.                 (eq (char-after (- (point) 2)) ?\.))
  435.               (forward-char -2)
  436.               ;; 97/3/14 jhod: Kinsoku
  437.               ;(skip-chars-backward "^ \n" linebeg)))
  438.               (fill-move-backward-to-break-point re-break-point linebeg)))
  439.         (if (featurep 'mule) (kinsoku-process))
  440.         ;end patch
  441.  
  442.         ;; If the left margin and fill prefix by themselves
  443.         ;; pass the fill-column. or if they are zero
  444.         ;; but we have no room for even one word,
  445.         ;; keep at least one word anyway.
  446.         ;; This handles ALL BUT the first line of the paragraph.
  447.         (if (if (zerop prefixcol)
  448.             (save-excursion
  449.               (skip-chars-backward " \t" linebeg)
  450.               (bolp))
  451.               (>= prefixcol (current-column)))
  452.             ;; Ok, skip at least one word.
  453.             ;; Meanwhile, don't stop at a period followed by one space.
  454.             (let ((first t))
  455.               (move-to-column prefixcol)
  456.               (while (and (not (eobp))
  457.                   (or first
  458.                       (and (not (bobp))
  459.                        sentence-end-double-space
  460.                        (save-excursion (forward-char -1)
  461.                                (and (looking-at "\\. ")
  462.                                 (not (looking-at "\\.  ")))))))
  463.             (skip-chars-forward " \t")
  464.             ;; 94/3/14 jhod: Kinsoku
  465.             ;(skip-chars-forward "^ \n\t")
  466.             (fill-move-forward-to-break-point re-break-point)
  467.             ;; end patch
  468.             (setq first nil)))
  469.           ;; Normally, move back over the single space between the words.
  470.           (if (eq (char-before (point)) ?\ )
  471.               (forward-char -1)))
  472.         ;; If the left margin and fill prefix by themselves
  473.         ;; pass the fill-column, keep at least one word.
  474.         ;; This handles the first line of the paragraph.
  475.         (if (and (zerop prefixcol)
  476.              (let ((fill-point (point)) nchars)
  477.                (save-excursion
  478.                  (move-to-left-margin)
  479.                  (setq nchars (- fill-point (point)))
  480.                  (or (< nchars 0)
  481.                  (and fill-prefix
  482.                       (< nchars (length fill-prefix))
  483.                       (string= (buffer-substring (point) fill-point)
  484.                            (substring fill-prefix 0 nchars)))))))
  485.             ;; Ok, skip at least one word.  But
  486.             ;; don't stop at a period followed by just one space.
  487.             (let ((first t))
  488.               (while (and (not (eobp))
  489.                   (or first
  490.                       (and (not (bobp))
  491.                        sentence-end-double-space
  492.                        (save-excursion (forward-char -1)
  493.                                (and (looking-at "\\. ")
  494.                                 (not (looking-at "\\.  ")))))))
  495.             (skip-chars-forward " \t")
  496.             ;; 97/3/14 jhod: Kinsoku
  497.             ;(skip-chars-forward "^ \t\n")
  498.             (fill-move-forward-to-break-point re-break-point)
  499.             ;; end patch
  500.             (setq first nil))))
  501.         ;; Check again to see if we got to the end of the paragraph.
  502.         (if (save-excursion (skip-chars-forward " \t") (eobp))
  503.             (or nosqueeze (delete-horizontal-space))
  504.           ;; Replace whitespace here with one newline, then indent to left
  505.           ;; margin.
  506.           (skip-chars-backward " \t")
  507.           ;; 97/3/14 jhod: More kinsoku stuff
  508.           (if (featurep 'mule)
  509.               ;; WAN means chars which match word-across-newline.
  510.               ;; (0)     | SPC + SPC* <EOB>    --> NL
  511.               ;; (1) WAN | SPC + SPC*        --> WAN + SPC + NL
  512.               ;; (2)     | SPC + SPC* + WAN    --> SPC + NL  + WAN
  513.               ;; (3) '.' | SPC + nonSPC        --> '.' + SPC + NL + nonSPC
  514.               ;; (4) '.' | SPC + SPC        --> '.' + NL
  515.               ;; (5)     | SPC*            --> NL
  516.               (let ((start (point))    ; 92.6.30 by K.Handa
  517.                 (ch (char-after (point))))
  518.             (if (and (= ch ? )
  519.                  (progn        ; not case (0) -- 92.6.30 by K.Handa
  520.                    (skip-chars-forward " \t")
  521.                    (not (eobp)))
  522.                  (or
  523.                   (progn    ; case (1)
  524.                     (goto-char start)
  525.                     (forward-char -1)
  526.                     (looking-at word-across-newline))
  527.                   (progn    ; case (2)
  528.                     (goto-char start)
  529.                     (skip-chars-forward " \t")
  530.                     (and (not (eobp))
  531.                      (looking-at word-across-newline)
  532.                      ;; never leave space after the end of sentence
  533.                      (not (fill-end-of-sentence-p))))
  534.                   (progn    ; case (3)
  535.                     (goto-char (1+ start))
  536.                     (and (not (eobp))
  537.                      (not (eq (char-after (point)) ? ))
  538.                      (fill-end-of-sentence-p)))))
  539.                 ;; We should keep one SPACE before NEWLINE. (1),(2),(3)
  540.                 (goto-char (1+ start))
  541.               ;; We should delete all SPACES around break point. (4),(5)
  542.               (goto-char start))))
  543.           ;; end of patch
  544.           (insert ?\n)
  545.           ;; Give newline the properties of the space(s) it replaces
  546.           (set-text-properties (1- (point)) (point)
  547.                        (text-properties-at (point)))
  548.           (indent-to-left-margin)
  549.           ;; Insert the fill prefix after indentation.
  550.           ;; Set prefixcol so whitespace in the prefix won't get lost.
  551.           (and fill-prefix (not (equal fill-prefix ""))
  552.                (progn
  553.              (insert fill-prefix)
  554.              (setq prefixcol (current-column))))))
  555.           ;; Justify the line just ended, if desired.
  556.           (if justify
  557.           (if (save-excursion (skip-chars-forward " \t") (eobp))
  558.               (progn
  559.             (delete-horizontal-space)
  560.             (justify-current-line justify t t))
  561.             (forward-line -1)
  562.             (justify-current-line justify nil t)
  563.             (forward-line 1))))))
  564.     ;; Leave point after final newline.
  565.     (goto-char (point-max)))
  566.     (forward-char 1))))
  567.  
  568. (defun fill-paragraph (arg)
  569.   "Fill paragraph at or after point.  Prefix arg means justify as well.
  570. If `sentence-end-double-space' is non-nil, then period followed by one
  571. space does not end a sentence, so don't break a line there.
  572.  
  573. If `fill-paragraph-function' is non-nil, we call it (passing our
  574. argument to it), and if it returns non-nil, we simply return its value."
  575.   (interactive (list (if current-prefix-arg 'full)))
  576.   (or (and fill-paragraph-function
  577.        (let ((function fill-paragraph-function)
  578.          fill-paragraph-function)
  579.          (funcall function arg)))
  580.       (let ((before (point)))
  581.     (save-excursion
  582.       (forward-paragraph)
  583.       (or (bolp) (newline 1))
  584.       (let ((end (point))
  585.         (beg (progn (backward-paragraph) (point))))
  586.         (goto-char before)
  587.         (if use-hard-newlines
  588.         ;; Can't use fill-region-as-paragraph, since this paragraph may
  589.         ;; still contain hard newlines.  See fill-region.
  590.         (fill-region beg end arg)
  591.           (fill-region-as-paragraph beg end arg)))))))
  592.  
  593. (defun fill-region (from to &optional justify nosqueeze to-eop)
  594.   "Fill each of the paragraphs in the region.
  595. Prefix arg (non-nil third arg, if called from program) means justify as well.
  596.  
  597. Noninteractively, fourth arg NOSQUEEZE non-nil means to leave
  598. whitespace other than line breaks untouched, and fifth arg TO-EOP
  599. non-nil means to keep filling to the end of the paragraph (or next
  600. hard newline, if `use-hard-newlines' is on).
  601.  
  602. If `sentence-end-double-space' is non-nil, then period followed by one
  603. space does not end a sentence, so don't break a line there."
  604.   (interactive
  605.    (progn
  606.      ;; XEmacs addition:
  607.      (barf-if-buffer-read-only nil (region-beginning) (region-end))
  608.      (list (region-beginning) (region-end)
  609.        (if current-prefix-arg 'full))))
  610.   (let (end beg)
  611.     (save-restriction
  612.       (goto-char (max from to))
  613.       (if to-eop
  614.       (progn (skip-chars-backward "\n")
  615.          (forward-paragraph)))
  616.       (setq end (point))
  617.       (goto-char (setq beg (min from to)))
  618.       (beginning-of-line)
  619.       (narrow-to-region (point) end)
  620.       (while (not (eobp))
  621.     (let ((initial (point))
  622.           end)
  623.       ;; If using hard newlines, break at every one for filling
  624.       ;; purposes rather than using paragraph breaks. 
  625.       (if use-hard-newlines
  626.           (progn 
  627.         (while (and (setq end (text-property-any (point) (point-max)
  628.                              'hard t))
  629.                 (not (eq ?\n (char-after end)))
  630.                 (not (= end (point-max))))
  631.           (goto-char (1+ end)))
  632.         (setq end (if end (min (point-max) (1+ end)) (point-max)))
  633.         (goto-char initial))
  634.         (forward-paragraph 1)
  635.         (setq end (point))
  636.         (forward-paragraph -1))
  637.       (if (< (point) beg)
  638.           (goto-char beg))
  639.       (if (>= (point) initial)
  640.           (fill-region-as-paragraph (point) end justify nosqueeze)
  641.         (goto-char end)))))))
  642.  
  643. ;; XEmacs addition: from Tim Bradshaw <tfb@edinburgh.ac.uk>
  644. (defun fill-paragraph-or-region (arg)
  645.   "Fill the current region, if it's active; otherwise, fill the paragraph.
  646. See `fill-paragraph' and `fill-region' for more information."
  647.   (interactive "*P")
  648.   (if (region-active-p)
  649.       (fill-region (point) (mark) arg)
  650.     (fill-paragraph arg)))
  651.  
  652.   
  653. (defconst default-justification 'left
  654.   "*Method of justifying text not otherwise specified.
  655. Possible values are `left', `right', `full', `center', or `none'.
  656. The requested kind of justification is done whenever lines are filled.
  657. The `justification' text-property  can locally override this variable.
  658. This variable automatically becomes buffer-local when set in any fashion.")
  659. (make-variable-buffer-local 'default-justification)
  660.  
  661. (defun current-justification ()
  662.   "How should we justify this line?
  663. This returns the value of the text-property `justification',
  664. or the variable `default-justification' if there is no text-property.
  665. However, it returns nil rather than `none' to mean \"don't justify\"."
  666.   (let ((j (or (get-text-property 
  667.         ;; Make sure we're looking at paragraph body.
  668.         (save-excursion (skip-chars-forward " \t") 
  669.                 (if (and (eobp) (not (bobp)))
  670.                     (1- (point)) (point)))
  671.         'justification)
  672.            default-justification)))
  673.     (if (eq 'none j)
  674.     nil
  675.       j)))
  676.  
  677. (defun set-justification (begin end value &optional whole-par)
  678.   "Set the region's justification style.
  679. The kind of justification to use is prompted for.
  680. If the mark is not active, this command operates on the current paragraph.
  681. If the mark is active, the region is used.  However, if the beginning and end
  682. of the region are not at paragraph breaks, they are moved to the beginning and
  683. end of the paragraphs they are in.
  684. If `use-hard-newlines' is true, all hard newlines are taken to be paragraph
  685. breaks.
  686.  
  687. When calling from a program, operates just on region between BEGIN and END,
  688. unless optional fourth arg WHOLE-PAR is non-nil.  In that case bounds are
  689. extended to include entire paragraphs as in the interactive command."
  690.   ;; XEmacs change (was mark-active)
  691.   (interactive (list (if (region-active-p) (region-beginning) (point))
  692.              (if (region-active-p) (region-end) (point))
  693.              (let ((s (completing-read
  694.                    "Set justification to: "
  695.                    '(("left") ("right") ("full")
  696.                  ("center") ("none"))
  697.                    nil t)))
  698.                (if (equal s "") (error ""))
  699.                (intern s))
  700.              t))
  701.   (save-excursion
  702.     (save-restriction
  703.       (if whole-par
  704.       (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
  705.         (paragraph-ignore-fill-prefix (if use-hard-newlines t 
  706.                         paragraph-ignore-fill-prefix)))
  707.         (goto-char begin)
  708.         (while (and (bolp) (not (eobp))) (forward-char 1))
  709.         (backward-paragraph)
  710.         (setq begin (point))
  711.         (goto-char end)
  712.         (skip-chars-backward " \t\n" begin)
  713.         (forward-paragraph)
  714.         (setq end (point))))
  715.  
  716.       (narrow-to-region (point-min) end)
  717.       (unjustify-region begin (point-max))
  718.       (put-text-property begin (point-max) 'justification value)
  719.       (fill-region begin (point-max) nil t))))
  720.  
  721. (defun set-justification-none (b e)
  722.   "Disable automatic filling for paragraphs in the region.
  723. If the mark is not active, this applies to the current paragraph."
  724.   ;; XEmacs change (was mark-active)
  725.   (interactive (list (if (region-active-p) (region-beginning) (point))
  726.              (if (region-active-p) (region-end) (point))))
  727.   (set-justification b e 'none t))
  728.  
  729. (defun set-justification-left (b e)
  730.   "Make paragraphs in the region left-justified.
  731. This is usually the default, but see the variable `default-justification'.
  732. If the mark is not active, this applies to the current paragraph."
  733.   ;; XEmacs change (was mark-active)
  734.   (interactive (list (if (region-active-p) (region-beginning) (point))
  735.              (if (region-active-p) (region-end) (point))))
  736.   (set-justification b e 'left t))
  737.  
  738. (defun set-justification-right (b e)
  739.   "Make paragraphs in the region right-justified:
  740. Flush at the right margin and ragged on the left.
  741. If the mark is not active, this applies to the current paragraph."
  742.   ;; XEmacs change (was mark-active)
  743.   (interactive (list (if (region-active-p) (region-beginning) (point))
  744.              (if (region-active-p) (region-end) (point))))
  745.   (set-justification b e 'right t))
  746.  
  747. (defun set-justification-full (b e)
  748.   "Make paragraphs in the region fully justified:
  749. This makes lines flush on both margins by inserting spaces between words.
  750. If the mark is not active, this applies to the current paragraph."
  751.   ;; XEmacs change (was mark-active)
  752.   (interactive (list (if (region-active-p) (region-beginning) (point))
  753.              (if (region-active-p) (region-end) (point))))
  754.   (set-justification b e 'full t))
  755.  
  756. (defun set-justification-center (b e)
  757.   "Make paragraphs in the region centered.
  758. If the mark is not active, this applies to the current paragraph."
  759.   ;; XEmacs change (was mark-active)
  760.   (interactive (list (if (region-active-p) (region-beginning) (point))
  761.              (if (region-active-p) (region-end) (point))))
  762.   (set-justification b e 'center t))
  763.  
  764. ;; 97/3/14 jhod: This functions are added for Kinsoku support
  765. (defun find-space-insertable-point ()
  766.  "Search backward for a permissable point for inserting justification spaces"
  767.  (if (boundp 'space-insertable)
  768.      (if (re-search-backward space-insertable nil t)
  769.      (progn (forward-char 1)
  770.         t)
  771.        nil)
  772.    (search-backward " " nil t)))
  773.  
  774. ;; A line has up to six parts:
  775. ;;
  776. ;;           >>>                    hello.                 
  777. ;; [Indent-1][FP][    Indent-2     ][text][trailing whitespace][newline]
  778. ;;
  779. ;; "Indent-1" is the left-margin indentation; normally it ends at column
  780. ;;     given by the `current-left-margin' function.
  781. ;; "FP" is the fill-prefix.  It can be any string, including whitespace.
  782. ;; "Indent-2" is added to justify a line if the `current-justification' is
  783. ;;     `center' or `right'.  In `left' and `full' justification regions, any
  784. ;;     whitespace there is part of the line's text, and should not be changed.
  785. ;; Trailing whitespace is not counted as part of the line length when
  786. ;; center- or right-justifying.
  787. ;;
  788. ;; All parts of the line are optional, although the final newline can 
  789. ;;     only be missing on the last line of the buffer.
  790.  
  791. (defun justify-current-line (&optional how eop nosqueeze)
  792.   "Do some kind of justification on this line.
  793. Normally does full justification: adds spaces to the line to make it end at
  794. the column given by `current-fill-column'.
  795. Optional first argument HOW specifies alternate type of justification:
  796. it can be `left', `right', `full', `center', or `none'.  
  797. If HOW is t, will justify however the `current-justification' function says to.
  798. If HOW is nil or missing, full justification is done by default.
  799. Second arg EOP non-nil means that this is the last line of the paragraph, so
  800. it will not be stretched by full justification.
  801. Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
  802. otherwise it is made canonical."
  803.   (interactive)
  804.   (if (eq t how) (setq how (or (current-justification) 'none))
  805.     (if (null how) (setq how 'full)
  806.       (or (memq how '(none left right center))
  807.       (setq how 'full))))
  808.   (or (memq how '(none left))  ; No action required for these.
  809.       (let ((fc (current-fill-column))
  810.         (pos (point-marker))
  811.         fp-end            ; point at end of fill prefix
  812.         beg                ; point at beginning of line's text
  813.         end                ; point at end of line's text
  814.         indent            ; column of `beg'
  815.         endcol            ; column of `end'
  816.         ncols)            ; new indent point or offset
  817.     (end-of-line)
  818.     ;; Check if this is the last line of the paragraph.
  819.     (if (and use-hard-newlines (null eop) 
  820.          (get-text-property (point) 'hard))
  821.         (setq eop t))
  822.     (skip-chars-backward " \t")
  823.     ;; Quick exit if it appears to be properly justified already
  824.     ;; or there is no text.
  825.     (if (or (bolp)
  826.         (and (memq how '(full right))
  827.              (= (current-column) fc)))
  828.         nil
  829.       (setq end (point))
  830.       (beginning-of-line)
  831.       (skip-chars-forward " \t")
  832.       ;; Skip over fill-prefix.
  833.       (if (and fill-prefix 
  834.            (not (string-equal fill-prefix ""))
  835.            (equal fill-prefix
  836.               (buffer-substring 
  837.                (point) (min (point-max) (+ (length fill-prefix)
  838.                                (point))))))
  839.           (forward-char (length fill-prefix))
  840.         (if (and adaptive-fill-mode 
  841.              (looking-at adaptive-fill-regexp))
  842.         (goto-char (match-end 0))))
  843.       (setq fp-end (point))
  844.       (skip-chars-forward " \t")
  845.       ;; This is beginning of the line's text.
  846.       (setq indent (current-column))
  847.       (setq beg (point))
  848.       (goto-char end)
  849.       (setq endcol (current-column))
  850.  
  851.       ;; HOW can't be null or left--we would have exited already
  852.       (cond ((eq 'right how) 
  853.          (setq ncols (- fc endcol))
  854.          (if (< ncols 0)
  855.              ;; Need to remove some indentation
  856.              (delete-region 
  857.               (progn (goto-char fp-end)
  858.                  (if (< (current-column) (+ indent ncols))
  859.                  (move-to-column (+ indent ncols) t))
  860.                  (point))
  861.               (progn (move-to-column indent) (point)))
  862.            ;; Need to add some
  863.            (goto-char beg)
  864.            (indent-to (+ indent ncols))
  865.            ;; If point was at beginning of text, keep it there.
  866.            (if (= beg pos) 
  867.                (move-marker pos (point)))))
  868.  
  869.         ((eq 'center how)
  870.          ;; Figure out how much indentation is needed
  871.          (setq ncols (+ (current-left-margin)
  872.                 (/ (- fc (current-left-margin) ;avail. space
  873.                       (- endcol indent)) ;text width
  874.                    2)))
  875.          (if (< ncols indent)
  876.              ;; Have too much indentation - remove some
  877.              (delete-region
  878.               (progn (goto-char fp-end)
  879.                  (if (< (current-column) ncols)
  880.                  (move-to-column ncols t))
  881.                  (point))
  882.               (progn (move-to-column indent) (point)))
  883.            ;; Have too little - add some
  884.            (goto-char beg)
  885.            (indent-to ncols)
  886.            ;; If point was at beginning of text, keep it there.
  887.            (if (= beg pos)
  888.                (move-marker pos (point)))))
  889.  
  890.         ((eq 'full how)
  891.          ;; Insert extra spaces between words to justify line
  892.          (save-restriction
  893.            (narrow-to-region beg end)
  894.            (or nosqueeze
  895.                (canonically-space-region beg end))
  896.            (goto-char (point-max))
  897.            (setq ncols (- fc endcol))
  898.            ;; Ncols is number of additional spaces needed
  899.            (if (> ncols 0)
  900.                (if (and (not eop)
  901.                 ;; 97/3/14 jhod: Kinsoku
  902.                 (find-space-insertable-point)) ;(search-backward " " nil t))
  903.                (while (> ncols 0)
  904.                  (let ((nmove (+ 3 (random 3))))
  905.                    (while (> nmove 0)
  906.                  (or (find-space-insertable-point) ;(search-backward " " nil t)
  907.                      (progn
  908.                        (goto-char (point-max))
  909.                        (find-space-insertable-point))) ;(search-backward " ")))
  910.                  (skip-chars-backward " ")
  911.                  (setq nmove (1- nmove))))
  912.                  ;; XEmacs change
  913.                  (insert " ")
  914.                  (skip-chars-backward " ")
  915.                  (setq ncols (1- ncols)))))))
  916.         (t (error "Unknown justification value"))))
  917.     (goto-char pos)
  918.     (move-marker pos nil)))
  919.   nil)
  920.  
  921. (defun unjustify-current-line ()
  922.   "Remove justification whitespace from current line.
  923. If the line is centered or right-justified, this function removes any
  924. indentation past the left margin.  If the line is full-justified, it removes
  925. extra spaces between words.  It does nothing in other justification modes."
  926.   (let ((justify (current-justification)))
  927.     (cond ((eq 'left justify) nil)
  928.       ((eq  nil  justify) nil)
  929.       ((eq 'full justify)        ; full justify: remove extra spaces
  930.        (beginning-of-line-text)
  931.        (canonically-space-region
  932.         (point) (save-excursion (end-of-line) (point))))
  933.       ((memq justify '(center right))
  934.        (save-excursion
  935.          (move-to-left-margin nil t)
  936.          ;; Position ourselves after any fill-prefix.
  937.          (if (and fill-prefix 
  938.               (not (string-equal fill-prefix ""))
  939.               (equal fill-prefix
  940.                  (buffer-substring 
  941.                   (point) (min (point-max) (+ (length fill-prefix)
  942.                               (point))))))
  943.          (forward-char (length fill-prefix)))
  944.          (delete-region (point) (progn (skip-chars-forward " \t")
  945.                        (point))))))))
  946.  
  947. (defun unjustify-region (&optional begin end)
  948.   "Remove justification whitespace from region.
  949. For centered or right-justified regions, this function removes any indentation
  950. past the left margin from each line.  For full-justified lines, it removes 
  951. extra spaces between words.  It does nothing in other justification modes.
  952. Arguments BEGIN and END are optional; default is the whole buffer."
  953.   (save-excursion
  954.     (save-restriction
  955.       (if end (narrow-to-region (point-min) end))
  956.       (goto-char (or begin (point-min)))
  957.       (while (not (eobp))
  958.     (unjustify-current-line)
  959.     (forward-line 1)))))
  960.  
  961.  
  962. (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
  963.   "Fill paragraphs within the region, allowing varying indentation within each.
  964. This command divides the region into \"paragraphs\",
  965. only at paragraph-separator lines, then fills each paragraph
  966. using as the fill prefix the smallest indentation of any line
  967. in the paragraph.
  968.  
  969. When calling from a program, pass range to fill as first two arguments.
  970.  
  971. Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
  972. JUSTIFY to justify paragraphs (prefix arg),
  973. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  974.   (interactive (list (region-beginning) (region-end)
  975.              (if current-prefix-arg 'full)))
  976.   (let ((fill-individual-varying-indent t))
  977.     (fill-individual-paragraphs min max justifyp mailp)))
  978.  
  979. (defun fill-individual-paragraphs (min max &optional justify mailp)
  980.   "Fill paragraphs of uniform indentation within the region.
  981. This command divides the region into \"paragraphs\",
  982. treating every change in indentation level as a paragraph boundary,
  983. then fills each paragraph using its indentation level as the fill prefix.
  984.  
  985. When calling from a program, pass range to fill as first two arguments.
  986.  
  987. Optional third and fourth arguments JUSTIFY and MAIL-FLAG:
  988. JUSTIFY to justify paragraphs (prefix arg),
  989. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  990.   (interactive (list (region-beginning) (region-end)
  991.              (if current-prefix-arg 'full)))
  992.   (save-restriction
  993.     (save-excursion
  994.       (goto-char min)
  995.       (beginning-of-line)
  996.       (narrow-to-region (point) max)
  997.       (if mailp 
  998.       (while (and (not (eobp))
  999.               (or (looking-at "[ \t]*[^ \t\n]+:")
  1000.               (looking-at "[ \t]*$")))
  1001.         (if (looking-at "[ \t]*[^ \t\n]+:")
  1002.         (search-forward "\n\n" nil 'move)
  1003.                 (forward-line 1))))
  1004.       (narrow-to-region (point) max)
  1005.       ;; Loop over paragraphs.
  1006.       (while (progn (skip-chars-forward " \t\n") (not (eobp)))
  1007.     (move-to-left-margin)
  1008.     (let ((start (point))
  1009.           fill-prefix fill-prefix-regexp)
  1010.       ;; Find end of paragraph, and compute the smallest fill-prefix
  1011.       ;; that fits all the lines in this paragraph.
  1012.       (while (progn
  1013.            ;; Update the fill-prefix on the first line
  1014.            ;; and whenever the prefix good so far is too long.
  1015.            (if (not (and fill-prefix
  1016.                  (looking-at fill-prefix-regexp)))
  1017.                (setq fill-prefix
  1018.                  (if (and adaptive-fill-mode adaptive-fill-regexp
  1019.                       (looking-at adaptive-fill-regexp))
  1020.                  (match-string 0)
  1021.                    (buffer-substring 
  1022.                 (point)
  1023.                 (save-excursion (skip-chars-forward " \t")
  1024.                         (point))))
  1025.                  fill-prefix-regexp (regexp-quote fill-prefix)))
  1026.            (forward-line 1)
  1027.            (if (bolp)
  1028.                ;; If forward-line went past a newline
  1029.                ;; move further to the left margin.
  1030.                (move-to-left-margin))
  1031.            ;; Now stop the loop if end of paragraph.
  1032.            (and (not (eobp))
  1033.             (if fill-individual-varying-indent
  1034.                 ;; If this line is a separator line, with or
  1035.                 ;; without prefix, end the paragraph.
  1036.                 (and 
  1037.                  (not (looking-at paragraph-separate))
  1038.                  (save-excursion
  1039.                    (not (and (looking-at fill-prefix-regexp)
  1040.                      ;; XEmacs change
  1041.                      (progn
  1042.                        (forward-char (length fill-prefix))
  1043.                        (looking-at paragraph-separate))))))
  1044.                             ;; If this line has more or less indent
  1045.                             ;; than the fill prefix wants, end the paragraph.
  1046.                             (and (looking-at fill-prefix-regexp)
  1047.                                  (save-excursion
  1048.                                    (not
  1049.                     (progn
  1050.                       (forward-char (length fill-prefix))
  1051.                       (or (looking-at paragraph-separate)
  1052.                       (looking-at paragraph-start))))))))))
  1053.       ;; Fill this paragraph, but don't add a newline at the end.
  1054.       (let ((had-newline (bolp)))
  1055.         (fill-region-as-paragraph start (point) justify)
  1056.         (or had-newline (delete-char -1))))))))
  1057.  
  1058. ;;; fill.el ends here
  1059.